import { getChannelList, updateSaasCompany } from '@/api/company'
import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import renderModal, {
    getModelValue,
    initModelValue,
    renderForm,
    type FormModalDto
} from '@/components/modal/Modal'
import toNative from '@/utils/toNative'
import { getReceiptData } from '@/utils/util'
import type { CompanyInfoDto } from '@/api/company/types'
import { message } from 'ant-design-vue'
@Component
export class DialogCheckInfoView extends Render {
    @Prop({ default: false }) show!: boolean
    @Prop() basicInfo!: CompanyInfoDto
    @Emit('update:show')
    showChange(val: boolean) {
        return val
    }
    @Emit('refresh')
    handleRefresh() {}
    @Watch('show', { immediate: true })
    async handleWatchShow() {
        this.formConfig.show = this.show
        if (this.show) {
            if (this.basicInfo.channelId) {
                this.formConfig.items.receiptId.options = await getReceiptData(
                    this.basicInfo.channelId
                )
            }
            initModelValue(this.formConfig, this.basicInfo)
        }
    }

    formConfig = this.initFormConfig() as FormModalDto

    initFormConfig() {
        return {
            title: '基本信息',
            width: '800px',
            theme: 'light',
            show: false,
            gutter: 20,
            items: {
                companyAbbreviation: {
                    name: '公司简称',
                    element: 'input',
                    span: 12
                },
                receiptId: {
                    name: '收款账户',
                    element: 'select',
                    span: 12,
                    params: {
                        defaultAttrs: {
                            label: 'bankName',
                            value: 'id'
                        }
                    },
                    callback: {
                        async method() {
                            return await getChannelList()
                        }
                    }
                },
                creditCode: {
                    name: '营业执照编码',
                    element: 'input',
                    span: 12
                },
                merchantBankCard: {
                    name: '客户充值账户',
                    element: 'input',
                    span: 12
                },
                businessLicenseAddress: {
                    name: '营业执照地址',
                    element: 'input',
                    span: 12
                },
                area: {
                    name: '所在区域',
                    element: 'input',
                    span: 12
                },
                legalPerson: {
                    name: '公司法人',
                    element: 'input',
                    span: 12
                },
                dailyQuota: {
                    name: '单日额度',
                    element: 'input',
                    span: 12
                },
                subMchid: {
                    name: '商户号',
                    element: 'input',
                    span: 12
                },
                personSingleQuota: {
                    name: '单日可领额度',
                    element: 'input',
                    span: 12
                }
            },
            complete: async () => {
                const data = getModelValue(this.formConfig)

                data.id = this.basicInfo.companyId
                data.channelId = this.basicInfo.channelId
                data.auditStatus = this.basicInfo.auditStatus

                await updateSaasCompany({
                    ...data
                })
                message.success('操作成功')
                this.handleRefresh()
                this.formConfig.show = false
                this.showChange(false)
            },
            cancel: () => {
                this.formConfig.show = false
                this.showChange(false)
            }
        } as FormModalDto
    }

    render() {
        return <div>{renderModal(this.formConfig)}</div>
    }
}

export default toNative<{ basicInfo: CompanyInfoDto }, { refresh: () => void }>(DialogCheckInfoView)
